package com.example.aop;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import java.util.Random;

@Component
@Aspect
public class MyAspect {
    @Pointcut("execution(* com.example.service.ProductService.*(..))")
    public void a(){}
    @Pointcut("execution(* com.example.service.ProductService.delete(..))")
                public void b(){}
    @Pointcut("execution(* com.example.service.ProductService.find*(..))")
                public void c(){}
    @AfterReturning(value="a()",returning="id")
    public void recordReturnValue(int id) {
        System.out.println("==============");
        System.out.println("添加成功，新产品的id为:" + id);
        System.out.println("==============");
    }
    @Before("a()||b()")
    public void log(){
        System.out.println("==============");
        System.out.println("日志增强");
        System.out.println("new Date()");
        System.out.println("==============");
    }
    @Before("a()||b()")
    public void checkRight(){
        System.out.println("==============");
        System.out.println("权限校验增强");
        Random r=new Random();
        if(r.nextInt(2)==0){
            System.out.println("校验失败");
            throw new RuntimeException("用户权限不足....");
        }else{
            System.out.println("校验通过");
        }
        System.out.println("new Date()");
        System.out.println("==============");
    }


}
